<?php


namespace app\api\controller\v1;


use app\api\model\Account;
use app\api\service\popularizeService;
use app\api\service\TimingService;
use think\facade\Request;

class Timing
{
    private $success;
    private $ip;

    /**
     * 限定ip
     * Timing constructor.
     */
    public function __construct()
    {
        $ip = Request::ip();
        if($ip !== config('host.ip')){
            $this->success = false;
        }
        $this->success = true;
    }

    /**
     * 自动任务(访问URL)， 没初始化每个账号可点击的次数（为8）
     */
    public function setAccountNum()
    {
        if($this->success == false){
            return $this->ip;
        }
        $allAccount = Account::getAllAccountName();
        $redis_hash_key = config('redis.account_num');
        foreach ($allAccount as $account){
            \Driver\Cache\Redis::hset($redis_hash_key, $account, 8);
        }
    }

    /**
     * 自动任务(访问URL) 更新注册表中每个手机号码对应贷款记录 （每四个小时更新一个当天所有的手机号码记录）
     * @return \think\response\Json
     */
    public function four()
    {
        if($this->success == false){
            return $this->ip;
        }
        $service = new TimingService();
        $result = $service->updatePhoneMoneyToday();
        return json($result);
    }

    /**
     * 自动任务(访问URL) 更新注册表中每个手机号码对应贷款记录 （每天更新一次当周）
     * @return \think\response\Json
     */
    public function week()
    {
        if($this->success == false){
            return $this->ip;
        }
        $service = new TimingService();
        $result = $service->updatePhoneMoneyLastSevenDay();
        return json($result);
    }

    /**
     * 同步每个账号到redis
     */
    public function setAccount()
    {
        if($this->success == false){
            return $this->ip;
        }
        $service = new TimingService();
        $result = $service->mysqlToRedis();
        return json($result);
    }

    /**
     * 获取今天每个账户所有流量的记录所有ip组成一个数组
     * 并写入到redis中
     * key => lqb_su_account_ip_
     * value => 所有ip集合
     */
    public function yesterday_ip()
    {
        if($this->success == false){
            return $this->ip;
        }
        $model = new Account();
        $arr =$model->getIps();
        $service = new TimingService();
        $result = $service->setAccountIps($arr);
        return json($result);
    }

    /**
     * 把redis_list里面为写入到mysql的全部写入
     */
    public function reg_list()
    {
        if($this->success == false){
            return $this->ip;
        }
        $arr = \Driver\Cache\Redis::getAllList(config('redis.reg'));
        $service = new popularizeService();
        $result = $service->listRegisteredToMysql($arr);
        return json($result);
    }

    /**
     * 定时任务,每天统计点击次数和申请次数
     */
    public function click()
    {
        if($this->success == false){
            return $this->ip;
        }
        $service = new TimingService();
        $result = $service->get_product_num();
        return json($result);
    }
}